package util;

import entity.text.ChapterEntity;
import entity.text.PaperEntity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author ：hzs
 * @date ：Created in 2021/2/5 15:50
 * @description：输入文章，计算每一节的keywords
 * @modified By：
 * @version: $
 */
public class TextRankKeywordsSimilarity {

    private static final int topK = 10;

    public static double getSimilarity(PaperEntity paper1, PaperEntity paper2) {
        return JaccardTextSimilarity.getSimilarity(getKeywordsString(paper1), getKeywordsString(paper2));
    }

    public static String getKeywordsString(PaperEntity paperEntity) {
        List<String> keywordsList = getKeywordsList(paperEntity);
        if (keywordsList == null) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : keywordsList) {
            stringBuilder.append(s).append(" ");
        }
        return stringBuilder.toString();
    }

    public static List<String> getKeywordsList(PaperEntity paperEntity) {
        List<List<String>> keywordsLists = getKeywordsLists(paperEntity);
        if (keywordsLists == null) {
            return null;
        }
        List<String> result = new ArrayList<>();
        for (List<String> keywordsList : keywordsLists) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String s : keywordsList) {
                stringBuilder.append(s).append(" ");
            }
            result.add(stringBuilder.toString());
        }
        return result;
    }

    public static List<List<String>> getKeywordsLists(PaperEntity paperEntity) {
        List<ChapterEntity> chapterEntities = paperEntity.getChapterEntities();
        if (chapterEntities == null) {
            return null;
        }
        List<List<String>> result = new ArrayList<>();
        int i = 1;
        while (true) {
            String title = "";
            String content = "";
            for (ChapterEntity chapterEntity : chapterEntities) {
                String index = chapterEntity.getIndex();
                if (String.valueOf(i).equals(index)) {
                    title = chapterEntity.getSubTitle();
                }
                if (index.equals(String.valueOf(i))
                        || index.startsWith(String.valueOf(i).concat("."))) {
                    content = content.concat(" ").concat(chapterEntity.getContent());
                }
            }
            if ("".equals(content)) {
                break;
            } else {
                Map<String, Float> keywords = TextRankKeyword.getKeyword(title, content);
                List<Map.Entry<String, Float>> entryList = new ArrayList<>(keywords.entrySet());
                Collections.sort(entryList, (a, b) -> a.getValue() - b.getValue() > 0 ? -1 : 1);
                List<String> words = new ArrayList<>();
                for (int j = 0; j < topK; j++) {
                    words.add(entryList.get(j).getKey());
                }
                result.add(words);
                i++;
            }
        }
        return result;
    }

}
